package com.sskj.postar.core.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

//import org.mybatis.spring.mapper.MapperScannerConfigurer;

/**
 * 这里我们重新配置MapperScannerConfigurer打开MybatisConfigurer
 * 将import org.mybatis.spring.mapper.MapperScannerConfigurer;
 * 修改为import tk.mybatis.spring.mapper.MapperScannerConfigurer;
 *
 */
@Configuration
@MapperScan(sqlSessionFactoryRef="sqlSessionFactoryBean",basePackages = "com.sskj.postar.mapper")
public class MyBatisConfig {
    @Bean(name = "sqlSessionFactoryBean")
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage("com.sskj.postar.model");
        // 添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        return factory.getObject();
    }

//    @Bean
//    public MapperScannerConfigurer mapperScannerConfigurer() {
//        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
//        mapperScannerConfigurer.setBasePackage("com.sskj.postar.dao");
//        return mapperScannerConfigurer;
//    }
}
